WOLF core
WOLF: A Modular Estimation Framework for Robotics
XXXXXXXXXXXXXXXXXXXXXXXXvoid(0)" frameborder="0" name="MSearchResults" id="MSearchResults">
solver_manager.h
Go to the documentation of this file.
1// WOLF - Copyright (C) 2020-2026
2// Institut de Robòtica i Informàtica Industrial, CSIC-UPC.
3// Authors: Joan Solà Ortega (jsola@iri.upc.edu) and
4// Joan Vallvé Navarro (jvallve@iri.upc.edu)
5// All rights reserved.
6//
7// This file is part of WOLF: http://www.iri.upc.edu/wolf
8// WOLF is free software: you can redistribute it and/or modify
9// it under the terms of the GNU General Public License version 3
10// as published by the Free Software Foundation.
11//
12// This program is distributed in the hope that it will be useful,
13// but WITHOUT ANY WARRANTY; without even the implied warranty of
14// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15// GNU General Public License for more details.
16//
17// You should have received a copy of the GNU General Public License
18// along with this program. If not, see <http://www.gnu.org/licenses/>.
19#pragma once
20
21// wolf includes
22#include "core/common/wolf.h"
27
28namespace wolf
29{
30WOLF_PTR_TYPEDEFS(SolverManager)
31
32/*
33 * Macro for defining Autoconf solver creator for WOLF's high level API.
34 *
35 * Place a call to this macro inside your class declaration (in the solver_class.h file),
36 * preferably just after the constructors. Inside 'public' keyword.
37 *
38 * In order to use this macro, the derived solver manager class, SolverClass,
39 * must have a constructor available with the API:
40 *
41 * SolverClass(const ProblemPtr& wolf_problem,
42 * const YAML::Node _params);
43 *
44 * Also, there should be the schema file 'SolverClass.schema' containing the specifications
45 * of the user input yaml file.
46 *
47 * NOTE: We use `new` and not `make_shared` since the constructor may (should) be private/protected
48 */
49#define WOLF_SOLVER_CREATE(SolverClass) \
50 static SolverManagerPtr create(const ProblemPtr& _problem, \
51 const YAML::Node& _input_node, \
52 const std::vector<std::string>& _folders_schema = {}) \
53 { \
54 if (not _folders_schema.empty()) \
55 { \
56 auto server = yaml_schema_cpp::YamlServer(_folders_schema); \
57 server.setYaml(_input_node); \
58 if (not server.applySchema(#SolverClass)) \
59 { \
60 WOLF_ERROR(server.getLog()); \
61 return nullptr; \
62 } \
63 return std::shared_ptr<SolverClass>(new SolverClass(_problem, server.getNode())); \
64 } \
65 return std::shared_ptr<SolverClass>(new SolverClass(_problem, _input_node)); \
66 } \
67 static SolverManagerPtr create(const ProblemPtr& _problem, \
68 const std::string& _yaml_filepath, \
69 const std::vector<std::string>& _folders_schema) \
70 { \
71 auto server = yaml_schema_cpp::YamlServer(_folders_schema, _yaml_filepath); \
72 if (not server.applySchema(#SolverClass)) \
73 { \
74 WOLF_ERROR(server.getLog()); \
75 return nullptr; \
76 } \
77 return create(_problem, server.getNode(), {}); \
78 }
79
84{
85 public:
88 enum class CovarianceBlocksToBeComputed : std::size_t
89 {
90 ALL = 0,
91 ALL_MARGINALS = 1,
92 ROBOT_LANDMARKS = 2,
94 GAUSS = 3,
95 GAUSS_TUCAN = 4,
96 GAUSS_TUCAN_ONLY_POSITION = 5
97 };
98
101 enum class ReportVerbosity : std::size_t
102 {
103 QUIET = 0,
104 BRIEF = 1,
105 FULL = 2
106 };
107
108 // PROFILING
111 void printProfiling(std::ostream& stream = std::cout) const;
112
115 const ProfilingUnit& getProfilingTotal() const;
116
119 const ProfilingUnit& getProfilingSolve() const;
120
123 const ProfilingUnit& getProfilingUpdate() const;
124
127 const ProfilingUnit& getProfilingState() const;
128
131 const ProfilingUnit& getProfilingCov() const;
132
133 protected:
134 ProblemPtr wolf_problem_;
135
143
144 YAML::Node params_;
145
146 private:
147 std::thread solver_thread_;
148
149 public:
153 SolverManager(const ProblemPtr& _problem);
157 SolverManager(const ProblemPtr& _problem, const YAML::Node& _params);
158
161 static SolverManagerPtr autoSetup(const ProblemPtr& _problem,
162 const std::string& _input_yaml_file,
163 std::vector<std::string> _primary_schema_folders,
164 bool _skip_yaml_validation = false);
165
168 static SolverManagerPtr autoSetup(const ProblemPtr& _problem,
169 YAML::Node _param_node,
170 std::vector<std::string> _primary_schema_folders,
171 bool _skip_yaml_validation = false);
172
175 virtual ~SolverManager();
176
180 std::string solve();
184 std::string solve(const ReportVerbosity report_level);
185
188 virtual bool computeCovariances() final;
189
192 virtual bool computeCovariances(const CovarianceBlocksToBeComputed blocks) final;
193
196 virtual bool computeCovariances(const std::vector<StateBlockPtr>& st_list) final;
197
201 virtual void update();
202
205 ProblemPtr getProblem();
206
209 YAML::Node getParams() const;
210
213 bool getComputeCov() const;
214
217 double getPeriod() const;
218
221 double getCovPeriod() const;
222
226
229 virtual bool isStateBlockRegistered(const StateBlockPtr& state_ptr) const final;
230
233 virtual bool isStateBlockFloating(const StateBlockPtr& state_ptr) const final;
234
237 virtual bool isStateBlockFixed(const StateBlockPtr& st) const final;
238
241 virtual bool isFactorRegistered(const FactorBasePtr& fac_ptr) const final;
242
245 virtual bool hasThisLocalParametrization(const StateBlockPtr& st,
246 const LocalParametrizationBasePtr& local_param) const final;
247
250 virtual bool hasLocalParametrization(const StateBlockPtr& st) const final;
251
254 virtual int numFactors() const final;
255
258 virtual int sizeFactors() const final;
259
262 virtual int numStateBlocks() const final;
263
266 virtual int sizeStateBlocks() const final;
267
270 virtual int numStateBlocksEstimated() const final;
271
274 virtual int sizeStateBlocksEstimated() const final;
275
278 virtual int numFactorsDerived() const = 0;
279
282 virtual int sizeFactorsDerived() const = 0;
283
286 virtual int numStateBlocksEstimatedDerived() const = 0;
287
290 virtual int sizeStateBlocksEstimatedDerived() const = 0;
291
294 virtual bool check(std::string prefix = "") const final;
295
296 protected:
297 std::map<StateBlockPtr, Eigen::VectorXd> state_blocks_;
298 std::map<StateBlockPtr, FactorBasePtrList> state_blocks_2_factors_;
299 std::set<FactorBasePtr> factors_;
300 std::set<StateBlockPtr> floating_state_blocks_;
301
304 virtual Eigen::VectorXd& getAssociatedMemBlock(const StateBlockPtr& state_ptr);
305
308 const double* getAssociatedMemBlockPtr(const StateBlockPtr& state_ptr) const;
309
312 double* getAssociatedMemBlockPtr(const StateBlockPtr& state_ptr);
313
314 private:
315 // SolverManager functions
318 virtual void addFactor(const FactorBasePtr& fac_ptr) final;
319
322 virtual void removeFactor(const FactorBasePtr& fac_ptr) final;
323
326 virtual void addStateBlock(const StateBlockPtr& state_ptr) final;
327
330 virtual void removeStateBlock(const StateBlockPtr& state_ptr) final;
331
334 virtual void updateStateBlockState(const StateBlockPtr& state_ptr) final;
335
338 virtual void updateStateBlockStatus(const StateBlockPtr& state_ptr) final;
339
342 virtual void updateStateBlockLocalParametrization(const StateBlockPtr& state_ptr) final;
343
344 // Pure virtual functions to be derived
345 protected:
348 virtual std::string solveDerived(const ReportVerbosity report_level) = 0;
349
353
356 virtual bool computeCovariancesDerived(const std::vector<StateBlockPtr>& st_list) = 0;
357
360 virtual void addFactorDerived(const FactorBasePtr& fac_ptr) = 0;
361
364 virtual void removeFactorDerived(const FactorBasePtr& fac_ptr) = 0;
365
368 virtual void addStateBlockDerived(const StateBlockPtr& state_ptr) = 0;
369
372 virtual void removeStateBlockDerived(const StateBlockPtr& state_ptr) = 0;
373
376 virtual void updateStateBlockStatusDerived(const StateBlockPtr& state_ptr) = 0;
377
380 virtual void updateStateBlockLocalParametrizationDerived(const StateBlockPtr& state_ptr) = 0;
381
384 virtual bool isStateBlockRegisteredDerived(const StateBlockPtr& state_ptr) const = 0;
385
388 virtual bool isFactorRegisteredDerived(const FactorBasePtr& fac_ptr) const = 0;
389
392 virtual bool isStateBlockFixedDerived(const StateBlockPtr& st) const = 0;
393
396 virtual bool hasLocalParametrizationDerived(const StateBlockPtr& st) const = 0;
397
400 virtual bool hasThisLocalParametrizationDerived(const StateBlockPtr& st,
401 const LocalParametrizationBasePtr& local_param) const = 0;
402
405 virtual void printProfilingDerived(std::ostream& stream = std::cout) const = 0;
406
409 virtual bool checkDerived(std::string prefix = "") const = 0;
410
411 public:
414 virtual bool converged() const = 0;
415
418 virtual bool failed() const = 0;
419
422 virtual bool wasStopped() const = 0;
423
426 virtual unsigned int iterations() const = 0;
427
430 virtual double initialCost() const = 0;
431
434 virtual double finalCost() const = 0;
435
438 virtual double totalTime() const = 0;
439
442 virtual void run() final;
443
446 virtual void stop() final;
447
448 private:
451 virtual void loop();
452
453 protected:
454 // PARAMS
455 double period_;
459 double cov_period_;
460 std::chrono::high_resolution_clock::time_point last_cov_time_;
462};
463
464} // namespace wolf
Profiling of a periodic task: number of calls, duration (accumulated, average, max,...
Definition: profiling_unit.h:31
Solver manager for WOLF.
Definition: solver_manager.h:84
virtual int numStateBlocksEstimated() const final
Returns the number of estimated state blocks: registered and not fixed.
Definition: solver_manager.cpp:608
ProfilingUnit profiling_cov_
Covariances computation.
Definition: solver_manager.h:142
virtual bool hasLocalParametrization(const StateBlockPtr &st) const final
Returns whether a state block has a local parametrization in the solver.
Definition: solver_manager.cpp:573
ProblemPtr wolf_problem_
Wolf problem.
Definition: solver_manager.h:134
virtual void stop() final
Stops the solver loop and joins its thread.
Definition: solver_manager.cpp:655
ProfilingUnit profiling_total_
Profiling units of solve(), solving, problem update, state update and covariances computation.
Definition: solver_manager.h:138
virtual int sizeFactorsDerived() const =0
Returns the total residual size of the factors in the derived solver.
virtual void updateStateBlockStatusDerived(const StateBlockPtr &state_ptr)=0
Updates the fixed/unfixed status of a state block in the derived solver.
virtual int sizeStateBlocksEstimatedDerived() const =0
Returns the total size of the estimated state blocks in the derived solver.
CovarianceBlocksToBeComputed
Enumeration of covariance blocks to be computed.
Definition: solver_manager.h:89
virtual bool isFactorRegisteredDerived(const FactorBasePtr &fac_ptr) const =0
Returns whether a factor is in the derived solver.
std::set< StateBlockPtr > floating_state_blocks_
State blocks not involved in any factor.
Definition: solver_manager.h:300
virtual Eigen::VectorXd & getAssociatedMemBlock(const StateBlockPtr &state_ptr)
Returns the state vector of a registered state block used by the solver.
Definition: solver_manager.cpp:497
ProfilingUnit profiling_state_
State update.
Definition: solver_manager.h:141
ProfilingUnit profiling_update_
Problem update.
Definition: solver_manager.h:140
virtual bool check(std::string prefix="") const final
Checks the consistency of the solver manager and of the derived solver.
Definition: solver_manager.cpp:718
YAML::Node getParams() const
Returns a copy of the YAML parameters.
Definition: solver_manager.cpp:624
virtual double initialCost() const =0
Returns the initial cost of the last solve.
ReportVerbosity getVerbosity() const
Returns the verbosity of the solver report.
Definition: solver_manager.cpp:533
virtual void run() final
Runs the solver loop in a separate thread.
Definition: solver_manager.cpp:644
std::chrono::high_resolution_clock::time_point last_cov_time_
Time of the last covariances computation.
Definition: solver_manager.h:460
const double * getAssociatedMemBlockPtr(const StateBlockPtr &state_ptr) const
Returns a pointer to the state vector of a registered state block used by the solver.
Definition: solver_manager.cpp:509
std::map< StateBlockPtr, FactorBasePtrList > state_blocks_2_factors_
Factors of each registered state block.
Definition: solver_manager.h:298
virtual void updateStateBlockLocalParametrizationDerived(const StateBlockPtr &state_ptr)=0
Updates the local parametrization of a state block in the derived solver.
virtual double finalCost() const =0
Returns the final cost of the last solve.
virtual bool checkDerived(std::string prefix="") const =0
Checks the consistency of the derived solver.
double getPeriod() const
Returns the period of the solver loop.
Definition: solver_manager.cpp:629
CovarianceBlocksToBeComputed cov_enum_
Covariance blocks computed in the solver loop.
Definition: solver_manager.h:458
virtual int sizeFactors() const final
Returns the total residual size of the registered factors.
Definition: solver_manager.cpp:587
bool compute_cov_
Whether the covariances are computed in the solver loop.
Definition: solver_manager.h:457
virtual unsigned int iterations() const =0
Returns the number of iterations of the last solve.
static SolverManagerPtr autoSetup(const ProblemPtr &_problem, const std::string &_input_yaml_file, std::vector< std::string > _primary_schema_folders, bool _skip_yaml_validation=false)
Creates a solver from a YAML file (see the YAML node version)
Definition: solver_manager.cpp:50
double getCovPeriod() const
Returns the period of the covariances computation in the solver loop.
Definition: solver_manager.cpp:634
virtual int numFactors() const final
Returns the number of registered factors.
Definition: solver_manager.cpp:582
virtual int numStateBlocks() const final
Returns the number of state blocks (registered and floating)
Definition: solver_manager.cpp:594
virtual int sizeStateBlocksEstimated() const final
Returns the total size of the estimated state blocks: registered and not fixed.
Definition: solver_manager.cpp:616
const ProfilingUnit & getProfilingUpdate() const
Returns the profiling of the problem update.
Definition: solver_manager.cpp:873
const ProfilingUnit & getProfilingTotal() const
Returns the profiling of solve(): update problem, solve and update state.
Definition: solver_manager.cpp:863
virtual bool hasLocalParametrizationDerived(const StateBlockPtr &st) const =0
Returns whether a state block has a local parametrization in the derived solver.
bool stopping_
Whether the solver loop has to stop.
Definition: solver_manager.h:461
virtual void addStateBlockDerived(const StateBlockPtr &state_ptr)=0
Adds a state block to the derived solver.
virtual int numFactorsDerived() const =0
Returns the number of factors in the derived solver.
virtual bool failed() const =0
Returns whether the last solve failed.
const ProfilingUnit & getProfilingCov() const
Returns the profiling of the covariances computation.
Definition: solver_manager.cpp:883
SolverManager(const ProblemPtr &_problem)
Constructor with default params_.
virtual bool computeCovariances() final
Computes the covariances of the blocks specified in the parameters. Returns whether it succeeded.
Definition: solver_manager.cpp:271
std::map< StateBlockPtr, Eigen::VectorXd > state_blocks_
Registered state blocks and solver states.
Definition: solver_manager.h:297
virtual bool hasThisLocalParametrizationDerived(const StateBlockPtr &st, const LocalParametrizationBasePtr &local_param) const =0
Returns whether a state block has a given local parametrization in the derived solver.
virtual bool isStateBlockRegistered(const StateBlockPtr &state_ptr) const final
Returns whether a state block is registered in the solver.
Definition: solver_manager.cpp:538
virtual bool isStateBlockFixedDerived(const StateBlockPtr &st) const =0
Returns whether a state block is fixed in the derived solver.
YAML::Node params_
YAML parameters.
Definition: solver_manager.h:144
bool getComputeCov() const
Returns whether the covariances are computed in the solver loop.
Definition: solver_manager.cpp:639
std::string solve()
Solves with the verbosity defined in params_.
Definition: solver_manager.cpp:229
virtual int sizeStateBlocks() const final
Returns the total size of the state blocks (registered and floating)
Definition: solver_manager.cpp:599
virtual double totalTime() const =0
Returns the total time of the last solve.
ProblemPtr getProblem()
Returns the wolf problem.
Definition: solver_manager.cpp:224
virtual bool isStateBlockFixed(const StateBlockPtr &st) const final
Returns whether a registered state block is fixed in the solver.
Definition: solver_manager.cpp:554
double cov_period_
Period of the covariances computation in the solver loop.
Definition: solver_manager.h:459
virtual bool isFactorRegistered(const FactorBasePtr &fac_ptr) const final
Returns whether a factor is registered in the solver.
Definition: solver_manager.cpp:549
virtual bool isStateBlockRegisteredDerived(const StateBlockPtr &state_ptr) const =0
Returns whether a state block is in the derived solver.
const ProfilingUnit & getProfilingSolve() const
Returns the profiling of the solving.
Definition: solver_manager.cpp:868
virtual void removeStateBlockDerived(const StateBlockPtr &state_ptr)=0
Removes a state block from the derived solver.
ProfilingUnit profiling_solve_
Solving.
Definition: solver_manager.h:139
virtual void update()
Updates solver's problem according to the wolf_problem.
Definition: solver_manager.cpp:121
void printProfiling(std::ostream &stream=std::cout) const
Prints the profiling of the solver manager (and of the derived solver)
Definition: solver_manager.cpp:888
virtual int numStateBlocksEstimatedDerived() const =0
Returns the number of estimated state blocks in the derived solver.
ReportVerbosity verbose_
Verbosity of the solver report.
Definition: solver_manager.h:456
virtual bool isStateBlockFloating(const StateBlockPtr &state_ptr) const final
Returns whether a state block is floating: not involved in any factor, so kept out of the solver.
Definition: solver_manager.cpp:544
virtual void printProfilingDerived(std::ostream &stream=std::cout) const =0
Prints the profiling of the derived solver.
virtual ~SolverManager()
Destructor.
Definition: solver_manager.cpp:48
std::set< FactorBasePtr > factors_
Registered factors.
Definition: solver_manager.h:299
virtual bool hasThisLocalParametrization(const StateBlockPtr &st, const LocalParametrizationBasePtr &local_param) const final
Returns whether a state block has a given local parametrization in the solver.
Definition: solver_manager.cpp:563
virtual bool computeCovariancesDerived(const CovarianceBlocksToBeComputed blocks)=0
Computes the covariances of the specified blocks in the derived solver.
virtual bool converged() const =0
Returns whether the last solve converged.
virtual bool wasStopped() const =0
Returns whether the last solve was stopped (interrupted by a problem change)
virtual void addFactorDerived(const FactorBasePtr &fac_ptr)=0
Adds a factor to the derived solver.
double period_
Period of the solver loop.
Definition: solver_manager.h:455
virtual std::string solveDerived(const ReportVerbosity report_level)=0
Solves the problem in the derived solver, returning the report.
virtual void removeFactorDerived(const FactorBasePtr &fac_ptr)=0
Removes a factor from the derived solver.
const ProfilingUnit & getProfilingState() const
Returns the profiling of the state update.
Definition: solver_manager.cpp:878
ReportVerbosity
Enumeration for the verbosity of the solver report.
Definition: solver_manager.h:102
Namespace extending Eigen definitions.
Definition: wolf.h:91
Definition: composite.h:251
Definition: jet.h:1355
Definition: capture_base.h:23
WOLF_PTR_TYPEDEFS(CaptureLandmarksExternal)